#ifndef __MY_THREAD_HPP__
#define __MY_THREAD_HPP__

#include <iostream>
#include <vector>
#include <string>
#include <pthread.h>
#include <functional>
#include <unistd.h>

using thread_func = std::function<void()>;
std::string threadnamedefault = "None-name";

class Thread
{
public:
    Thread(thread_func func, std::string name = threadnamedefault)
        : _func(func),
          _name(name),
          _isrunning(false)
    {
        std::cout << "creat thread obj sucuss" << std::endl;
    }
    ~Thread()
    {
    }

    static void *start_routine(void *arge)
    {
        Thread *self = static_cast<Thread *>(arge);
        self->_isrunning = true;
        self->_func();
        pthread_exit((void *)0);
    }
    // 创建线程
    void Start()
    {
        int n = pthread_create(&_tid, nullptr, start_routine, this);
        if (n == 0)
        {
            std::cout << "run thread sucuss" << std::endl;
        }
    }
    // 回收线程
    void Jion()
    {
        _isrunning = false;
        int n = pthread_join(_tid, nullptr);
        if (n == 0)
        {
            std::cout << "join thread sucuss" << std::endl;
        }
    }

private:
    pthread_t _tid;
    bool _isrunning;
    std::string _name;
    thread_func _func;
};

#endif